package com.bosssoft.g1.authserver.component;

import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class ContextHolder {

    /**
     * 存放token认证
     */
    private static final ConcurrentHashMap<String, String> CONTEXT_MAP = new ConcurrentHashMap<>();

    public void setUserCode(String code, String token) {
        CONTEXT_MAP.put(code, token);
    }

    // 新增的删除方法
    public String removeUserCode(String code) {
        return CONTEXT_MAP.remove(code);
    }

    public String getUserCode(String code) {
        return CONTEXT_MAP.get(code);
    }

    public void clear() {
        CONTEXT_MAP.clear();
    }

    public boolean hasValue(String token) {
        return CONTEXT_MAP.containsValue(token);
    }

    public void printAllKeysAndValues() {
        for (Map.Entry<String, String> entry : CONTEXT_MAP.entrySet()) {
            System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
        }
    }
}
